#! /usr/bin/perl -w

use strict;
use Carp;

use IO::Socket;
use XML::Simple;
use Data::Dumper;

use constant BUFSIZE   => 2048;
use constant SLEEPTIME => 10;

my $host = shift or die "Usage: client.pl host [port]\n";
my $port = shift || '10280';

#while (1) {
    my $socket = new IO::Socket(
        Domain   => PF_INET,
        PeerAddr => $host,
        PeerPort => $port,
        Proto    => getprotobyname('tcp'),
        Timeout  => 60,
    ) or die $@;

    talk_to_server($socket);

    close($socket);
    #sleep(SLEEPTIME);
#}

sub talk_to_server {
    if (@_>1){carp "Only can have one input, not @_.";}
    my ($server) = @_;

    my $buffer;
    $server->recv( $buffer, BUFSIZE, 0 );
    print "---Server: ".$buffer;

    my $data2server = obtain_data2server();

    print Dumper($data2server);
    $server->send( $data2server, 0 );

#   $server->recv( $buffer, BUFSIZE, 0 );
#   deal_with_order($buffer);

    $server->recv( $buffer, BUFSIZE, 0 );
    print "---Server: ".$buffer;
}

sub obtain_data2server {
    if (@_>0){carp "Only can have 0 input, not @_.";}
    my $xml = new XML::Simple;
    my $data;
    my $newdata;
    $data = $xml->XMLin("request.xml");
    $newdata = $xml->XMLout( $data, NoAttr => 1, RootName => 'queueinfo' );
    return $newdata;
}
